<?php

namespace App\Http\Controllers\Admin;

use App\Http\Model\Admin;
use App\Http\Model\Article;
use App\Http\Model\Images;
use App\Http\Model\Logs;
use App\Http\Model\User;
use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Session;

class AdminController extends Controller
{
    /**
     * 管理员信息
     * @return $this|array
     */
    public function adminInfo(){
        check_auth();
        if($input=Input::except('_token')){
            $id=Session::get('admin')['id'];
            if(Admin::where('id',$id)->update($input)){
                return ['status'=>1,'msg'=>'更新成功'];
            }else{
                return ['status'=>0,'msg'=>'更新失败'];
            }
        }
        $logs=Logs::limit(50)->get()->all();
        $num['user']=User::count();
        $num['author']=User::where(['is_author'=>1])->count();
        $num['article']=Article::count();
        $num['images']=Images::count();
        return view('admin.admininfo',compact('logs','num'))->with(['title'=>'管理员信息']);
    }

    /**
     * 备份数据库
     * @return array
     */
    public function backDB(){
        set_time_limit(4*3600*10);
        $shell="mysqldump -uroot -proot alpaca >D:\\phpStudy\\WWW\\alpaca\\DB_back\\".date('Y_m_d_H_i_s').".sql";
        exec($shell);
        return [
            'status'=>1,
            'msg'=>'备份成功'
        ];
    }
}
